/**
 * 日志工具
 */

/**
 * 检查是否应该显示日志
 */
function shouldLog(level) {
  const logLevel = import.meta.env.VITE_LOG_LEVEL || (import.meta.env.DEV ? 'debug' : 'error')
  const levels = ['debug', 'info', 'warn', 'error']
  const currentLevelIndex = levels.indexOf(logLevel)
  const requestedLevelIndex = levels.indexOf(level)
  return requestedLevelIndex >= currentLevelIndex
}

export function info(message, ...args) {
  if (shouldLog('info')) {
    console.info(`[INFO]`, message, ...args)
  }
}

export function error(message, ...args) {
  if (shouldLog('error')) {
    console.error(`[ERROR]`, message, ...args)
  }
}

export function warn(message, ...args) {
  if (shouldLog('warn')) {
    console.warn(`[WARN]`, message, ...args)
  }
}

export function debug(message, ...args) {
  if (shouldLog('debug')) {
    console.debug(`[DEBUG]`, message, ...args)
  }
}

export const logger = { info, error, warn, debug }
export default logger
